from game_api.serializer import *

#游戏排行榜
class gameTop():
    def __init__(self):
        pass
    # 游戏列表
    def list(self, req):
        tag = req['tag']
        #热门排行
        if  tag=='top':
            a = Game.objects.order_by('-downloaded').all()
        #最新排行
        elif tag=='hot':
            a = Game.objects.order_by('-touched').all()
        else:
            a = Game.objects.all()
        res_data=a
        return res_data

    # 查询属于该分类下的游戏信息
    def catelist(self, res):
        id = res['id']
        a = GameClass.objects.get(id=id).rel_game.all()
        l = []
        for x in a:
            # name = {'name': x.belong_game.name}
            name = GameSerializer(x.belong_game).data
            down=GameDownSerializer(x.belong_game.gamedown).data
            name.update(GameDetailSerializer(x).data)
            name.update(down)
            l.append(name)
        res_data = l
        return res_data

    # 查询所有分类下的所有游戏信息
    def catelists(self,):
        a = GameClass.objects.all()
        l = []
        for x in a:
            c = GameClassSerializer(x).data
            c['game']=[]
            for i in x.rel_game.all():
                b = GameSerializer(i.belong_game).data
                b.update(GameDetailSerializer(i).data)
                c['game'].append(b)
            l.append(c)
        res_data = l
        return res_data


    # 查询单个游戏
    def onedetail(self, id):
        a = Game.objects.get(id=id)
        res_data = a
        return res_data

    # 查询多个游戏
    def manygame(self, req):
        name = req['name']
        a = Game.objects.filter(name__icontains=name)
        res_data = a
        return res_data

     # 查询推荐的游戏
    def tuijian(self, req):
        a = Game.objects.filter(top=1)
        res_data = a
        return res_data

    # 查询游戏详情
    def detail(self,dat,type):
        #查询单个游戏详情
        if type=='one':
            b = GameSerializer(dat).data
            c = GameDetailSerializer(dat.gamedetail).data
            d = GameDownSerializer(dat.gamedown).data
            b.update(c)
            d.update(b)
            res_data=d
        #查询多个游戏详情
        elif type=='many':
            l=[]
            for i in dat:
                b = GameSerializer(i).data
                c = GameDetailSerializer(i.gamedetail).data
                d = GameDownSerializer(i.gamedown).data
                b.update(c)
                d.update(b)
                l.append(d)
            res_data = l
        return res_data

